import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 
 */

/**
 * @author Adrien
 */
public class Main
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		File fichierSortie = new File(System.getProperty("user.dir")
				+ "/rscript.txt");
		try
		{
			FileWriter fw = new FileWriter(fichierSortie);
			BufferedWriter writer = new BufferedWriter(fw);

			// workspace
			writer.write("setwd(\"C:/R/\")");
			writer.newLine();

			// initMoyennes
			for (int algo = 1; algo <= 2; algo++)
			{
				for (int temps = 1; temps <= 2; temps++)
				{
					writer.write("moyTemps" + String.valueOf(temps) + "_Low_"
							+ String.valueOf(algo) + "<- 0");
					writer.newLine();
					writer.write("moyTemps" + String.valueOf(temps) + "_Med_"
							+ String.valueOf(algo) + "<- 0");
					writer.newLine();
					writer.write("moyTemps" + String.valueOf(temps) + "_Hig_"
							+ String.valueOf(algo) + "<- 0");
					writer.newLine();
				}
			}

			// corps
			for (int algo = 1; algo <= 2; algo++)
				for (int numFichier = 1; numFichier <= 100; numFichier++)
				{
					// on charge le fichier Low
					writer.write("data<-read.table(\"out"
							+ String.valueOf(algo) + "_Low_"
							+ String.valueOf(numFichier)
							+ ".csv\",sep=\";\",header=TRUE)");
					writer.newLine();
					// on modifie les moyennes Low
					for (int temps = 1; temps <= 2; temps++)
					{
						writer.write("moyTemps" + String.valueOf(temps)
								+ "_Low_" + String.valueOf(algo) + "<- "
								+ "moyTemps" + String.valueOf(temps) + "_Low_"
								+ String.valueOf(algo) + " + mean(data$Temps"
								+ String.valueOf(temps) + ")");
						writer.newLine();
					}
					// fichier Med
					writer.write("data<-read.table(\"out"
							+ String.valueOf(algo) + "_Med_"
							+ String.valueOf(numFichier)
							+ ".csv\",sep=\";\",header=TRUE)");
					writer.newLine();
					for (int temps = 1; temps <= 2; temps++)
					{
						writer.write("moyTemps" + String.valueOf(temps)
								+ "_Med_" + String.valueOf(algo) + "<- "
								+ "moyTemps" + String.valueOf(temps) + "_Med_"
								+ String.valueOf(algo) + " + mean(data$Temps"
								+ String.valueOf(temps) + ")");
						writer.newLine();
					}
					writer.write("data<-read.table(\"out"
							+ String.valueOf(algo) + "_Hig_"
							+ String.valueOf(numFichier)
							+ ".csv\",sep=\";\",header=TRUE)");
					writer.newLine();
					for (int temps = 1; temps <= 2; temps++)
					{
						writer.write("moyTemps" + String.valueOf(temps)
								+ "_Hig_" + String.valueOf(algo) + "<- "
								+ "moyTemps" + String.valueOf(temps) + "_Hig_"
								+ String.valueOf(algo) + " + mean(data$Temps"
								+ String.valueOf(temps) + ")");
						writer.newLine();
					}
				}

			// divisions des moyennes
			for (int algo = 1; algo <= 2; algo++)
			{
				for (int temps = 1; temps <= 2; temps++)
				{
					writer.write("moyTemps" + String.valueOf(temps) + "_Low_"
							+ String.valueOf(algo) + "<- " + "moyTemps"
							+ String.valueOf(temps) + "_Low_"
							+ String.valueOf(algo) + "/100");
					writer.newLine();
					writer.write("moyTemps" + String.valueOf(temps) + "_Med_"
							+ String.valueOf(algo) + "<- " + "moyTemps"
							+ String.valueOf(temps) + "_Med_"
							+ String.valueOf(algo) + "/100");
					writer.newLine();
					writer.write("moyTemps" + String.valueOf(temps) + "_Hig_"
							+ String.valueOf(algo) + "<- " + "moyTemps"
							+ String.valueOf(temps) + "_Hig_"
							+ String.valueOf(algo) + "/100");
					writer.newLine();
				}
			}

			// creation des tableaux
			for (int algo = 1; algo <= 2; algo++){
				writer.write("moy_algo" + String.valueOf(algo)
						+ "<- data.frame(Temps1=c(moyTemps1_Low_"+String.valueOf(algo)+",moyTemps1_Med_"+String.valueOf(algo)+",moyTemps1_Hig_"+String.valueOf(algo)+")," 
						+ " Temps2=c(moyTemps2_Low_"+String.valueOf(algo)+",moyTemps2_Med_"+String.valueOf(algo)+",moyTemps2_Hig_"+String.valueOf(algo)+"), "
						+ "TempsTotal=c(moyTemps1_Low_"+String.valueOf(algo)+"+moyTemps2_Low_"+String.valueOf(algo)+",moyTemps1_Med_"+String.valueOf(algo)+" + moyTemps2_Med_"+String.valueOf(algo)+",moyTemps1_Hig_"+String.valueOf(algo)+" + moyTemps2_Hig_"+String.valueOf(algo)+"),"
						+ " row.names=c(\"Faible\",\"Moyen\",\"Intense\"))");
				writer.newLine();
			}
			
			for (int taux = 1; taux <= 3; taux++){
				String tauxString;
				switch(taux){
				case 1:
					tauxString = "Low";
					break;
				case 2:
					tauxString = "Med";
					break;
				case 3:
					tauxString = "Hig";
					break;
				default:
					tauxString = "";
					break;
				}
				
				writer.write("moy_taux"
						+ tauxString
						+ "<- data.frame(Temps1=c(moyTemps1_"+ tauxString +"_1,moyTemps1_"+ tauxString +"_2)," +
								" Temps2=c(moyTemps2_"+ tauxString +"_1,moyTemps2_"+ tauxString +"_2), " +
								"TempsTotal=c(moyTemps1_"+ tauxString +"_1+moyTemps2_"+ tauxString +"_1,moyTemps1_"+ tauxString +"_2 + moyTemps2_"+ tauxString +"_2)," +
								" row.names=c(\"Algo3\",\"Algo6\"))");
				writer.newLine();
			}

			for (int temps = 1; temps <= 2; temps++){
				writer.write("moy_temps"
						+ String.valueOf(temps)
						+ "<- data.frame(Algo3=c(moyTemps"+ String.valueOf(temps) +"_Low_1,moyTemps"+ String.valueOf(temps) +"_Med_1,moyTemps"+ String.valueOf(temps) +"_Hig_1)," +
								" Algo6=c(moyTemps"+ String.valueOf(temps) +"_Low_2,moyTemps"+ String.valueOf(temps) +"_Med_2,moyTemps"+ String.valueOf(temps) +"_Hig_2)," +
								" row.names=c(\"Low\",\"Med\",\"Hig\"))");
				writer.newLine();
			}
			//temps total
			writer.write("moy_tempsTotal"
					+ "<- data.frame(Algo3=c(moyTemps1_Low_1+moyTemps2_Low_1,moyTemps1_Med_1+moyTemps2_Med_1,moyTemps1_Hig_1+moyTemps2_Hig_1)," +
							" Algo6=c(moyTemps1_Low_2+moyTemps2_Low_2,moyTemps1_Med_2+moyTemps2_Med_2,moyTemps1_Hig_2+moyTemps2_Hig_2)," +
							" row.names=c(\"Low\",\"Med\",\"Hig\"))");
			writer.newLine();

			writer.close();
		} catch (IOException e)
		{
			System.err.println("Erreur creation FileWriter 1");
			e.printStackTrace();
		}

	}
}
